package com.example.demo.dao;


import com.example.demo.Enity.User;
import com.example.demo.Enity.privateList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.ArrayList;

public interface ChatlistJpa extends JpaRepository<privateList, Integer>, JpaSpecificationExecutor<privateList> {
    //查看是否第一次跟该用户聊天
    @Query(nativeQuery = true, value = "select count(*) from private_list where (mineid=?1 or mineid=?2) and (otherid=?2 or otherid=?1) and mineid != otherid ")
    public int getsum(String sender, String toUser);

    //自己聊天
    @Query(nativeQuery = true, value = "select count(*) from private_list where (mineid=?1 or mineid=?2) and (otherid=?2 or otherid=?1)")
    public int getsum2(String sender, String toUser);

    //获取与自己聊过天的用户信息
    @Query(nativeQuery = true, value = "select U.img,U.name,U.wxid,R.message from private_list R join consumer U on R.otherid=U.wxid where R.mineid=?1")
    ArrayList getmypeople(String sender);

    @Query(nativeQuery = true, value = "select U.img,U.name,U.wxid,R.message from private_list R join consumer U on R.mineid=U.wxid where R.otherid=?1")
    ArrayList getotherpeople(String sender);

    //修改最近聊天内容(与他人)
    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "update private_list set message=?3 where (mineid=?1 or mineid=?2) and (otherid=?2 or otherid=?1) and mineid != otherid")
    public void changeRelate(String myid, String otherid, String message);

    //修改最近聊天内容(与自己)
    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "update private_list set message=?3 where (mineid=?1 or mineid=?2) and (otherid=?2 or otherid=?1)")
    public void changeRelate2(String myid, String otherid, String message);

    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "delete from private_list where (mineid=?1 and otherid=?2) or (mineid=?2 and otherid=?1)")
    void deleteChatlist(String sender,String toUser);
}
